#light

open System
open System.Text

type Color =
    | Black
    | White

type PieceType =
    | Pawn
    | King

type Piece(color : Color, pieceType : PieceType) =
    member piece.Color = color
    member piece.PieceType = pieceType
    static member OppositeColor color =
        match color with
            | Black -> White
            | White -> Black
    static member YDirection color =
        match color with
            | Black -> -1
            | White ->  1

let (|Piece|) (piece : Piece) = Piece(piece.Color, piece.PieceType)

type Board(width : int, height : int, currentPlayer : Color) =
    let squares = Array2.create width height (None:Piece option)
    let toChar x y =
        match squares.[x, y] with
            | Some(Piece(Black, Pawn)) -> 'b'
            | Some(Piece(Black, King)) -> 'B'
            | Some(Piece(White, Pawn)) -> 'k'
            | Some(Piece(White, King)) -> 'K'
            | None -> if ((x + y) % 2 = 0) then '+' else '-'
    member board.Item
        with get(x, y) = squares.[x, y]
        and  set(x, y, piece) = squares.[x, y] <- piece
    override board.ToString() =
        let sb = new StringBuilder()
        let mutable y = height
        sb.AppendLine() |> ignore
        while (y > 0) do
            y <- y - 1
            let mutable x = 0
            while (x < width) do
                sb.Append(toChar x y) |> ignore
                x <- x + 1
            sb.AppendLine() |> ignore
        sb.ToString()
